package network;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.sql.SQLOutput;
import java.util.Scanner;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class TcpEchoServer {
    private ServerSocket serverSocket = null;
    public TcpEchoServer(int port) throws IOException {
        serverSocket = new ServerSocket(port);
    }
    //1.普通版本
    /**public void start() throws IOException{
     System.out.println("服务器启动!");

     while(true){
     Socket clientSocket = serverSocket.accept();
     processConnection(clientSocket);
     }
     }**/
    //2.多线程版本
    /**public void start() throws IOException{
        System.out.println("服务器启动!");
        while(true){
            Socket clientSocket = serverSocket.accept();
            Thread t = new Thread(() ->{
                try {
                    processConnection(clientSocket);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            });
            t.start();
        }
    }**/
    //线程池版本
    public void  start() throws IOException{
        System.out.println("服务器启动!");
        ExecutorService service = Executors.newCachedThreadPool();
        while (true){
            Socket clientsocket = serverSocket.accept();
            service.submit(new Runnable() {
                @Override
                public void run() {
                    try {
                        processConnection(clientsocket);
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                }
            });

        }



    }

    private void processConnection(Socket clientSocket) throws IOException {
        System.out.printf("[%s:%d]客户端上线!\n",clientSocket.getInetAddress(),clientSocket.getPort());
        try (InputStream inputStream  = clientSocket.getInputStream();
             OutputStream outputStream = clientSocket.getOutputStream()){
            Scanner scanner = new Scanner(inputStream);
            while(true){
                if (!scanner.hasNext()){
                    System.out.printf("[%s:%d]客户端下线!\n",clientSocket.getInetAddress(),clientSocket.getPort());
                    break;
                }
                String request = scanner.next();
                String response = process(request);
                outputStream.write(response.getBytes(),0,request.getBytes().length);
                System.out.printf("[%s:%d]req=%s,req=%s\n",clientSocket.getInetAddress(),clientSocket.getPort(),request,response);
            }
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            clientSocket.close();
        }
    }
    public String process(String request) {
        return request +"\n";
    }
    public static void main(String[] args) throws IOException{
        TcpEchoServer server = new TcpEchoServer(9090);
        server.start();
    }
}
